package com.xdy.redis.demo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2023-05-07  16:24
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderService {

    private static final String ORDER_KEY = "order:";

    @Resource
    private RedisTemplate redisTemplate;
//    private StringRedisTemplate stringRedisTemplate;

    public void addOrder() {
        int keyId = ThreadLocalRandom.current().nextInt(1000) + 1;
        String orderNo = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(ORDER_KEY + keyId, "京东订单_" + orderNo);
        log.info("新增订单====>编号：{},的订单流水号：{}", keyId, orderNo);
    }

    public String getOrder(Integer keyId) {
        String orderNo = (String) redisTemplate.opsForValue().get(ORDER_KEY + keyId);
        log.info("查询订单====>编号：{},的订单流水号：{}", keyId, orderNo);
        return orderNo;
    }
}
